﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.IO;
using System.Text;
using CetWin.Phone.Entity;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.Micro;
namespace CetWin
{
    public class WordIOService
    {

        private CetWinDbDataContext db;


        public WordIOService()
        {
            db = CetWinDbDataContext.GetNewDataContext();
        }

        public IList<Word> LoadFromText(string file)
        {
            ObservableCollection<Word> list = new ObservableCollection<Word>();
            if (db.Categories.Any(e => e.Name == file))
            {
                list = new ObservableCollection<Word>(db.Words.Where(e => e.Category == file));

                return list;
            }
            //MessageBox.Show("x");
            Uri fileUri = new Uri(string.Format("resources//{0}.txt", file), UriKind.Relative);
            StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);

            if ((streamInfo != null) && (streamInfo.Stream != null))
            {
                using (StreamReader reader = new StreamReader(streamInfo.Stream, Encoding.UTF8))
                {
                    string str = null;
                    while ((str = reader.ReadLine()) != null)
                    {
                        list.Add(new Word() { Name = str, Explain = reader.ReadLine(), Category = file });
                    }
                }
            }
            db.Categories.InsertOnSubmit(new Category() { Name = file });
            db.SubmitChanges();
            db.Words.InsertAllOnSubmit(list);
            db.SubmitChanges();
            return list;
        }

        public IList<Word> LoadLibrary(string file)
        {
            ObservableCollection<Word> list = new ObservableCollection<Word>();
            Uri fileUri = new Uri(string.Format("resources//{0}.txt", file), UriKind.Relative);
            StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);
          
            if ((streamInfo != null) && (streamInfo.Stream != null))
            {
                
                using (StreamReader reader = new StreamReader(streamInfo.Stream, Encoding.UTF8))
                {
                    string str = null;
                    while ((str = reader.ReadLine()) != null)
                    {
                        list.Add(new Word() { Name = str });
                    }
                }
            }

            return list;
        }

        public IList<Word> LoadFromText1()
        {
         
            //if (db.Categories.Any(e => e.Name == "baseWord"))
            //{
            //    list = new ObservableCollection<Word>(db.Words.Where(e => e.Category == "baseWord"));

            //    return list;
            //}
            //MessageBox.Show("x");
            if (db.Words.Count() == 0)
            {
                ObservableCollection<Word> list = new ObservableCollection<Word>();
                Uri fileUri = new Uri("resources//baseWord.txt", UriKind.Relative);
                StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(fileUri);

                if ((streamInfo != null) && (streamInfo.Stream != null))
                {
                    using (StreamReader reader = new StreamReader(streamInfo.Stream, Encoding.UTF8))
                    {
                        string str = null;
                        while ((str = reader.ReadLine()) != null)
                        {
                            var strs = str.Split(new char[] { '|' });
                            if (strs.Length == 4)
                            {
                                list.Add(new Word()
                                {
                                    Name = strs[0],
                                    Explain = strs[1],
                                    Pron = strs[2],
                                    Category=strs[3],
                                    WordExtesion = new WordExtesion() { Name = strs[0] }
                                });
                                //    list2.Add(new WordExtesion() { Comment = strs[3], Name = strs[0] });
                            }
                        }
                    }
                }

                db.Words.InsertAllOnSubmit(list);
                db.SubmitChanges();
            }
            return db.Words.OrderBy(e=>e.Name).ToList();
        }
    }
}

namespace Phone.Framework.CaliburnExtensions
{
    public class CabiburnService
    {

        public static Type GetType(DependencyObject obj)
        {
            return (Type)obj.GetValue(TypeProperty);
        }

        public static void SetType(DependencyObject obj, Type value)
        {
            obj.SetValue(TypeProperty, value);
        }

        public static readonly DependencyProperty TypeProperty =
            DependencyProperty.RegisterAttached("Type", typeof(Type), typeof(CabiburnService),
            new PropertyMetadata((sender, args) =>
            {

                var element = sender as FrameworkElement;
                if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(element)) return;
                var val = args.OldValue as Type;
                Caliburn.Micro.Action.SetTargetWithoutContext(element, IoC.GetInstance(val, null));

            }));

        public static string GetTypeWithDataContext(DependencyObject obj)
        {
            return (string)obj.GetValue(TypeWithDataContextProperty);
        }

        public static void SetTypeWithDataContext(DependencyObject obj, string value)
        {
            obj.SetValue(TypeWithDataContextProperty, value);
        }

        public static readonly DependencyProperty TypeWithDataContextProperty =
            DependencyProperty.RegisterAttached("TypeWithDataContext", typeof(string), typeof(CabiburnService),
            new PropertyMetadata((sender, args) =>
            {

                var element = sender as FrameworkElement;
                if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(element)) return;

                var val = Type.GetType(args.NewValue.ToString());
                element.DataContext = IoC.GetInstance(val, null);

            }));
    }
}